using Assets.Scripts.DL.Consts;
using Assets.Scripts.DL.Enemy.CommonEnemy;
using Assets.Scripts.DL.Mgrs;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;

namespace DL {
	public class BloodBar : MonoBehaviour {
		// Start is called before the first frame update
		[SerializeField] Image fast;
		[SerializeField] Image slow;
		[SerializeField] float lerpValue = 0.05f;
		[SerializeField] Controller EnemyController;
		bool _isSlowChanging = false;
		public int maxHP;

		void Start() {
			EnemyController = GetComponentInParent<Controller>();
			maxHP = EnemyController.Config.MaxHP;
		}

		// Update is called once per frame
		void Update() {
			fast.fillAmount = 1.0f * EnemyController.StateData.CurHP / maxHP;
			if (!_isSlowChanging) {
				StartCoroutine(Slow());
			}
		}

		//Test
		//int _maxHP = 100;
		//int _curHP = 100;
		//void Update() {
		//	if (Input.GetKeyDown(KeyCode.Space)) {
		//		_curHP -= 10;
		//		var before = fast.fillAmount;
		//		fast.fillAmount = 1.0f * _curHP / _maxHP;
		//		var after = fast.fillAmount;
		//		PoolTools.MakeBloodBarEffect(transform.parent, GetComponent<RectTransform>(), before, after);
		//	}
		//	//if (!_isSlowChanging) {
		//	//	StartCoroutine(Slow());
		//	//}
		//}

		IEnumerator Slow() {
			_isSlowChanging = true;
			while (slow.fillAmount > fast.fillAmount) {
				slow.fillAmount = Mathf.Lerp(slow.fillAmount, fast.fillAmount, lerpValue);
				yield return null;
			}
			_isSlowChanging = false;
		}

	}

}
